home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / include / TFileSpec.h < prev    next >
Text File  |  1995-11-05  |  8KB  |  288 lines

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    TFileSpec.h        -    C and C++ routines to deal with path names
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log: TFileSpec.h,v $
  8. Revision 1.4  1994/12/31  00:29:31  neeri
  9. FSSpec encoding.
  10.  
  11. Revision 1.3  1994/08/10  00:37:29  neeri
  12. Sanitized for universal headers.
  13.  
  14. Revision 1.2  1994/05/01  23:51:12  neeri
  15. Added TFileSpec(const TFileSpec &) copy constructor.
  16.  
  17. Revision 1.1  1994/02/25  02:57:43  neeri
  18. Initial revision
  19.  
  20. Revision 0.12  1993/09/27  00:00:00  neeri
  21. FSpSmartMove(), Special constructors
  22.  
  23. Revision 0.11  1993/07/17  00:00:00  neeri
  24. TFileSpec::LastInfo()
  25.  
  26. Revision 0.10  1993/03/14  00:00:00  neeri
  27. 1.1.0 Baseline
  28.  
  29. Revision 0.9  1993/02/06  00:00:00  neeri
  30. DefaultDir()
  31.  
  32. Revision 0.8  1993/01/15  00:00:00  neeri
  33. IsParentOf, operator ==
  34.  
  35. Revision 0.7  1993/01/03  00:00:00  neeri
  36. Prevent multiple includes
  37.  
  38. Revision 0.6  1992/12/08  00:00:00  neeri
  39. Made Default() public
  40.  
  41. Revision 0.5  1992/11/15  00:00:00  neeri
  42. Made suitable for use from C programs
  43.  
  44. Revision 0.4  1992/11/15  00:00:00  neeri
  45. Renamed once again
  46.  
  47. Revision 0.3  1992/11/15  00:00:00  neeri
  48. Forgot a few consts
  49.  
  50. Revision 0.2  1992/09/12  00:00:00  neeri
  51. Renamed
  52.  
  53. Revision 0.1  1992/09/08  00:00:00  neeri
  54. Permission flags were the wrong way around
  55.  
  56. *********************************************************************/
  57.  
  58. #ifndef _TFILESPEC_
  59. #define _TFILESPEC_
  60.  
  61. /* These routines run both under System 7 and under older systems. */
  62.  
  63. #include <Files.h>
  64. #include <Folders.h>
  65.  
  66. #define kTempFileType    'TMPF'
  67.  
  68. #ifdef __cplusplus
  69.  
  70. /************************** The C++ only interface ***************************/
  71.  
  72.  
  73. #if PRAGMA_ALIGN_SUPPORTED
  74. #pragma options align=mac68k
  75. #endif
  76. class TFileSpec : public FSSpec {
  77. protected:
  78.     static OSErr        error;
  79.  
  80.     void    Root();
  81.     Boolean IsRoot();
  82. private:
  83.     static short        curVol;
  84.     static long            curDir;
  85.     static CInfoPBRec    lastInfo;
  86.  
  87.     OSErr    DefaultDir();
  88.     OSErr    FindVol(short index = -1);
  89.     Boolean    IsEncodedFSSpec(const char * path, Boolean useAlias);
  90.     char *    RelPath(short curRef, long curDirID) const;
  91. public:
  92.     // Return last error
  93.     static OSErr        Error()                            {    return error;        }
  94.  
  95.     // Return last info. Valid after [], Exists
  96.     static const CInfoPBRec * LastInfo()            {    return &lastInfo;    }
  97.     
  98.     // Change current directory
  99.     static OSErr        ChDir(const TFileSpec & spec);
  100.  
  101.     // Set to current directory
  102.     OSErr    Default();
  103.  
  104.     TFileSpec()                                                                {}
  105.  
  106.     // Construct from TFileSpec
  107.     TFileSpec(const TFileSpec & spec);
  108.     
  109.     // Construct from FSSpec
  110.     TFileSpec(const FSSpec & spec, Boolean useAlias = false);
  111.  
  112.     // Construct from components
  113.     TFileSpec(short vRefNum, long parID, ConstStr31Param name, Boolean useAlias = false);
  114.  
  115.     // Construct from working directory & file name
  116.     TFileSpec(short wd, ConstStr31Param name, Boolean useAlias = false);
  117.  
  118.     // Construct from full or relative path
  119.     TFileSpec(const char * path, Boolean useAlias = false);
  120.  
  121.     // Construct using FindFolder()    
  122.     TFileSpec(OSType object, short vol = kOnSystemDisk, long dir = 0);
  123.     
  124.     // Construct from open file reference number
  125.     TFileSpec(short fRefNum);
  126.     
  127.     // This is currently an expensive no-op, but if you call this before passing
  128.     // the FSSpec to a FSp routine, you are guaranteed to remain compatible forever.
  129.     void Bless();
  130.  
  131.     // Give full pathname
  132.     char *    FullPath() const;
  133.  
  134.     // If TFileSpec is an alias, return path it points to 
  135.     // without mounting any volumes. Otherwise, return nil
  136.     char *    FullAliasPath() const;
  137.  
  138.     // Give path relative to current directory (as defined by HSetVol())
  139.     char *    RelPath() const;
  140.     // Give path relative to given directory
  141.     char *    RelPath(const FSSpec & dir) const;
  142.  
  143.     // Give encoded path. Encoding is:
  144.     //
  145.     // 1 byte:   DC1  (ASCII 0x11)
  146.     // 4 bytes:  Volume reference number in zero-padded hex
  147.     // 8 bytes:  Directory ID in zero-padded hex
  148.     // n bytes:  Partial pathname, starting with ':'
  149.     //
  150.     // Needless to say, passing encoded values to anything but a GUSI routine is 
  151.     // a bad idea.
  152.     char *    Encode() const;
  153.     
  154.     // Give information about the current object. If dirInfo is true, give information
  155.     //   about the current object's directory.
  156.     OSErr        CatInfo(CInfoPBRec & info, Boolean dirInfo = false) const;
  157.  
  158.     // If object is an alias file, resolve it. If gently is true, nonexisting files
  159.     //   are tolerated.
  160.     OSErr        Resolve(Boolean gently = true);
  161.  
  162.     // Resolve an existing object for which we already have a CInfoPBRec
  163.     OSErr        Resolve(const CInfoPBRec & info);
  164.  
  165.     // true if object exists
  166.     Boolean    Exists() const;
  167.  
  168.     // true if object is a parent directory of other
  169.     Boolean    IsParentOf(const TFileSpec & other) const;
  170.  
  171.     // Replace object with its parent directory
  172.     TFileSpec     operator--();
  173.  
  174.     // Get (vRefNum, parID) of directory object
  175.     TFileSpec     operator++();
  176.  
  177.     // Equivalent to calling -- levels times
  178.     TFileSpec     operator-=(int levels);
  179.  
  180.     // Equivalent to calling -= on a *copy* of the current object
  181.     TFileSpec    operator-(int levels) const;
  182.  
  183.     // Replace directory object by object with given name inside the directory
  184.     TFileSpec    AddPathComponent(const char * name, int length);
  185.     TFileSpec     operator+=(ConstStr31Param name);
  186.     TFileSpec    operator+=(const char * name);
  187.  
  188.     // Non-destructive version of +=
  189.     TFileSpec    operator+(ConstStr31Param name) const;
  190.     TFileSpec    operator+(const char * name) const;
  191.  
  192.     // Return the index-th object in the *parent* directory of the current object
  193.     TFileSpec     operator[](short index) const;
  194.  
  195.     // Return if the two filespecs (not) are equal
  196.     Boolean        operator==(const TFileSpec & other) const;
  197.     Boolean        operator!=(const TFileSpec & other) const;
  198. };
  199. #if PRAGMA_ALIGN_SUPPORTED
  200. #pragma options align=reset
  201. #endif
  202.  
  203. inline Boolean IsFile(const CInfoPBRec & info)
  204. {
  205.     return !(info.dirInfo.ioFlAttrib & 0x10);
  206. }
  207.  
  208. inline Boolean IsAlias(const CInfoPBRec & info)
  209. {
  210.     return
  211.         !(info.hFileInfo.ioFlAttrib & 0x10) &&
  212.         (info.hFileInfo.ioFlFndrInfo.fdFlags & (1 << 15));
  213. }
  214.  
  215. inline Boolean DirIsExported(const CInfoPBRec & info)
  216. {
  217.     return (info.hFileInfo.ioFlAttrib & 0x20);
  218. }
  219.  
  220. inline Boolean DirIsMounted(const CInfoPBRec & info)
  221. {
  222.     return (info.hFileInfo.ioFlAttrib & 0x08);
  223. }
  224.  
  225. inline Boolean DirIsShared(const CInfoPBRec & info)
  226. {
  227.     return (info.hFileInfo.ioFlAttrib & 0x04);
  228. }
  229.  
  230. inline Boolean HasRdPerm(const CInfoPBRec & info)
  231. {
  232.     return !(info.dirInfo.ioACUser & 0x02);
  233. }
  234.  
  235. inline Boolean HasWrPerm(const CInfoPBRec & info)
  236. {
  237.     return !(info.dirInfo.ioACUser & 0x04);
  238. }
  239.  
  240. extern "C" {
  241. #endif
  242.  
  243. /* Routines shared between C and C++ */
  244.  
  245. /* Convert a working directory & file name into a FSSpec. */
  246. OSErr WD2FSSpec(short wd, ConstStr31Param name, FSSpec * desc);
  247.  
  248. /* Convert a FSSpec into a full pathname. */
  249. char * FSp2FullPath(const FSSpec * desc);
  250.  
  251. /* Works like FSp2FullPath, but creates a *relative* pathname if the object
  252.    is contained in the current directory.
  253. */
  254. char * FSp2RelPath(const FSSpec * desc);
  255.  
  256. /* Works like FSp2RelPath, but regarding to a specified directory.
  257. */
  258. char * FSp2DirRelPath(const FSSpec * desc, const FSSpec * dir);
  259.  
  260. /* Encode FSSpec as described above for TFileSpec::Encode */
  261. char * FSp2Encoding(const FSSpec * desc);
  262.  
  263. /* Call GetCatInfo for file system object. */
  264. OSErr    FSpCatInfo(const FSSpec * desc, CInfoPBRec * info);
  265.  
  266. /* Return FSSpec of (vRefNum, parID) */
  267. OSErr FSpUp(FSSpec * desc);
  268.  
  269. /* Return FSSpec of file in directory denoted by desc */
  270. OSErr FSpDown(FSSpec * desc, ConstStr31Param name);
  271.  
  272. /* Return FSSpec of nth file in directory denoted by (vRefNum, parID) */
  273. OSErr FSpIndex(FSSpec * desc, short n);
  274.  
  275. /* Convert a pathname into a file spec. */
  276. OSErr Path2FSSpec(const char * path, FSSpec * desc);
  277.  
  278. /* Convert a special object into a file spec. */
  279. OSErr Special2FSSpec(OSType object, short vol, long dirID, FSSpec * desc);
  280.  
  281. /* Move & Rename File */
  282. OSErr FSpSmartMove(const FSSpec * from, const FSSpec * to);
  283.  
  284. #ifdef __cplusplus
  285. }
  286. #endif
  287. #endif
  288.